﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section14
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            Translate()
            hidMSDSID.Value = IIf(IsNothing(Request.QueryString("Id")), 0, CInt(Request.QueryString("Id")))
            ShowData()
        End If
    End Sub
    Protected Sub Translate()
        lblSection14Header.Text = getTranslate("lblSection14Header", Session("LanguageId"), lblSection14Header.Text)
        lblHeaderCol1.Text = getTranslate("lblHeaderCol1", Session("LanguageId"), lblHeaderCol1.Text)
        lblHeaderCol2.Text = getTranslate("lblHeaderCol2", Session("LanguageId"), lblHeaderCol2.Text)
        lblHeaderCol3.Text = getTranslate("lblHeaderCol3", Session("LanguageId"), lblHeaderCol3.Text)
        lblHeaderCol4.Text = getTranslate("lblHeaderCol4", Session("LanguageId"), lblHeaderCol4.Text)
        lblHeaderCol5.Text = getTranslate("lblHeaderCol5", Session("LanguageId"), lblHeaderCol5.Text)
        lblHeaderCol6.Text = getTranslate("lblHeaderCol6", Session("LanguageId"), lblHeaderCol6.Text)
        lblHeaderCol7.Text = getTranslate("lblHeaderCol7", Session("LanguageId"), lblHeaderCol7.Text)
        'lblSection14Row1.Text = getTranslate("lblSection14Row1", Session("LanguageId"), lblSection14Row1.Text)
        'lblSection14Row2.Text = getTranslate("lblSection14Row2", Session("LanguageId"), lblSection14Row2.Text)
        'lblSection14Row3.Text = getTranslate("lblSection14Row3", Session("LanguageId"), lblSection14Row3.Text)
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            'Tiêu đề doanh nghiệp
            Dim lang = CInt(Session("LanguageId"))
            Dim Id As Integer = hidMSDSID.Value
            Dim p As MSDSSection14 = (From a In data.MSDSSection14 Where a.MSDSId = Id And a.LanguageId = lang).FirstOrDefault
            If Not IsNothing(p) Then
                hidID.Value = p.Section14Id

                'Content
                txtS1411.Text = IIf(IsNothing(p.S1411) = True, "", p.S1411)
                txtS1412.Text = IIf(IsNothing(p.S1412) = True, "", p.S1412)
                txtS1413.Text = IIf(IsNothing(p.S1413) = True, "", p.S1413)
                txtS1414.Text = IIf(IsNothing(p.S1414) = True, "", p.S1414)
                txtS1415.Text = IIf(IsNothing(p.S1415) = True, "", p.S1415)
                txtS1416.Text = IIf(IsNothing(p.S1416) = True, "", p.S1416)
                txtS1421.Text = IIf(IsNothing(p.S1421) = True, "", p.S1421)
                txtS1422.Text = IIf(IsNothing(p.S1422) = True, "", p.S1422)
                txtS1423.Text = IIf(IsNothing(p.S1423) = True, "", p.S1423)
                txtS1424.Text = IIf(IsNothing(p.S1424) = True, "", p.S1424)
                txtS1425.Text = IIf(IsNothing(p.S1425) = True, "", p.S1425)
                txtS1426.Text = IIf(IsNothing(p.S1426) = True, "", p.S1426)
                txtS1431.Text = IIf(IsNothing(p.S1431) = True, "", p.S1431)
                txtS1432.Text = IIf(IsNothing(p.S1432) = True, "", p.S1432)
                txtS1433.Text = IIf(IsNothing(p.S1433) = True, "", p.S1433)
                txtS1434.Text = IIf(IsNothing(p.S1434) = True, "", p.S1434)
                txtS1435.Text = IIf(IsNothing(p.S1435) = True, "", p.S1435)
                txtS1436.Text = IIf(IsNothing(p.S1436) = True, "", p.S1436)

                txtS1441.Text = IIf(IsNothing(p.S1441) = True, "", p.S1441)
                'Label
                Translate()
            End If
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Try
                Dim p As MSDSSection14
                p = (From q In data.MSDSSection14
                Where q.Section14Id = hidID.Value
                  ).FirstOrDefault()
                Dim _isNew = True
                If Not p Is Nothing Then
                    _isNew = False
                Else
                    p = New MSDSSection14
                End If
                With p
                    .S1411 = txtS1411.Text.Trim()
                    .S1412 = txtS1412.Text.Trim()
                    .S1413 = txtS1413.Text.Trim()
                    .S1414 = txtS1414.Text.Trim()
                    .S1415 = txtS1415.Text.Trim()
                    .S1416 = txtS1416.Text.Trim()
                    .S1421 = txtS1421.Text.Trim()
                    .S1422 = txtS1422.Text.Trim()
                    .S1423 = txtS1423.Text.Trim()
                    .S1424 = txtS1424.Text.Trim()
                    .S1425 = txtS1425.Text.Trim()
                    .S1426 = txtS1426.Text.Trim()
                    .S1431 = txtS1431.Text.Trim()
                    .S1432 = txtS1432.Text.Trim()
                    .S1433 = txtS1433.Text.Trim()
                    .S1434 = txtS1434.Text.Trim()
                    .S1435 = txtS1435.Text.Trim()
                    .S1436 = txtS1436.Text.Trim()
                    .S1441 = txtS1441.Text.Trim()

                    .LanguageId = Session("LanguageId")
                    .MSDSId = hidMSDSID.Value
                End With
                If _isNew Then
                    p.Created = Now
                    p.Creator = Session("UserName")
                    data.MSDSSection14.AddObject(p)
                Else
                    p.Modified = Now
                    p.Modifier = Session("UserName")
                End If
                data.SaveChanges()

                Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section14:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section15.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub
    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("../../Page/MSDS/Section13.aspx?Id=" & hidMSDSID.Value)
    End Sub
#End Region
End Class
